load("@fbcode_macros//build_defs:cpp_binary.bzl", "cpp_binary")
load("@fbcode_macros//build_defs:cpp_library.bzl", "cpp_library")
load("//fboss:THIRD-PARTY-VERSIONS.bzl", "BCM_SDKS", "SAI_BRCM_DNX_IMPLS", "to_impl_suffix", "to_versions")
load("//fboss/agent/hw/sai/impl:impl.bzl", "SAI_BRCM_IMPLS", "SAI_LEABA_IMPLS", "SAI_NVIDIA_IMPLS", "get_link_group_map", "to_impl_bin_name", "to_impl_lib_name")
load("//fboss/agent/hw/sai/switch:switch.bzl", "sai_switch_dependent_name")

oncall("fboss_agent_push")

cpp_library(
    name = "agent_hw_test_src",
    srcs = [
        "Agent2QueueToOlympicQoSTests.cpp",
        "AgentAclCounterTests.cpp",
        "AgentAclInDiscardCounterTests.cpp",
        "AgentAclPriorityTests.cpp",
        "AgentAmIdlesTests.cpp",
        "AgentAqmTests.cpp",
        "AgentArsBase.cpp",
        "AgentArsFlowletTest.cpp",
        "AgentConfigSetupTest.cpp",
        "AgentConfigVerifyQosTests.cpp",
        "AgentCoppTests.cpp",
        "AgentDeepPacketInspectionTests.cpp",
        "AgentDiagShellStressTests.cpp",
        "AgentDot1qMappingTest.cpp",
        "AgentDscpMarkingTests.cpp",
        "AgentDscpQueueMappingTests.cpp",
        "AgentEcmpSpilloverTests.cpp",
        "AgentEcmpTests.cpp",
        "AgentEgressForwardingDiscardCounterTests.cpp",
        "AgentEmptyTests.cpp",
        "AgentFabricSwitchTests.cpp",
        "AgentFlowletSwitchingTests.cpp",
        "AgentHighFrequencyStatsTests.cpp",
        "AgentHwAclMatchActionsTest.cpp",
        "AgentHwAclQualifierTest.cpp",
        "AgentHwAclStatTests.cpp",
        "AgentHwParityErrorTests.cpp",
        "AgentHwPtpTcProvisionTests.cpp",
        "AgentHwPtpTcTests.cpp",
        "AgentHwResourceStatsTests.cpp",
        "AgentHwUdfTests.cpp",
        "AgentInNullRouteDiscardsTest.cpp",
        "AgentInTrapDiscardsTest.cpp",
        "AgentIngressBufferTests.cpp",
        "AgentInterPacketGapTests.cpp",
        "AgentIpInIpTunnelTests.cpp",
        "AgentJumboFramesTests.cpp",
        "AgentL3ForwardingTests.cpp",
        "AgentL4PortBlackholingTests.cpp",
        "AgentLoadBalancerTests.cpp",
        "AgentLoopBackTests.cpp",
        "AgentMacLearningAndNeighborResolutionTests.cpp",
        "AgentMacLearningTests.cpp",
        "AgentMemoryUsageTests.cpp",
        "AgentMirrorOnDropTests.cpp",
        "AgentMirroringScaleTests.cpp",
        "AgentMirroringTests.cpp",
        "AgentMmuTuningTests.cpp",
        "AgentNSFScaleTests.cpp",
        "AgentNeighborTests.cpp",
        "AgentNetworkAIQosSchedulerTests.cpp",
        "AgentNetworkAIQosTests.cpp",
        "AgentOlympicQosSchedulerTests.cpp",
        "AgentOlympicQosTests.cpp",
        "AgentOverflowTestBase.cpp",
        "AgentPacketSendTests.cpp",
        "AgentPfcTests.cpp",
        "AgentPortBandWidthTests.cpp",
        "AgentPortLedTests.cpp",
        "AgentPrbsTests.cpp",
        "AgentQosSchedulerTestBase.cpp",
        "AgentQosTestBase.cpp",
        "AgentQueuePerHostL2Tests.cpp",
        "AgentQueuePerHostRouteTests.cpp",
        "AgentQueuePerHostTests.cpp",
        "AgentRouteOverDifferentAddressFamilyNhopTests.cpp",
        "AgentRouteOverflowTests.cpp",
        "AgentRouteTests.cpp",
        "AgentRouterInterfaceCounterTest.cpp",
        "AgentRxReasonTests.cpp",
        "AgentSendPacketToQueueTests.cpp",
        "AgentSflowMirrorTest.cpp",
        "AgentStateReconstructionTests.cpp",
        "AgentSwitchStateReplayTest.cpp",
        "AgentSwitchStatsTxCounterTests.cpp",
        "AgentTrafficPauseTests.cpp",
        "AgentTrafficPfcTests.cpp",
        "AgentTrunkLoadBalancerTests.cpp",
        "AgentTrunkTests.cpp",
        "AgentTunnelMgrTests.cpp",
        "AgentVoqConditionalEntropyTests.cpp",
        "AgentVoqSwitchFirmwareTests.cpp",
        "AgentVoqSwitchFullScaleDsfTests.cpp",
        "AgentVoqSwitchInterruptsTests.cpp",
        "AgentVoqSwitchLineRateTests.cpp",
        "AgentVoqSwitchTests.cpp",
        "AgentVoqSwitchWithFabricPortsTests.cpp",
        "AgentVoqSwitchWithMultipleDsfNodesTests.cpp",
        "AgentWatermarkTests.cpp",
    ],
    headers = [
        "AgentArsBase.h",
    ],
    link_whole = True,
    exported_deps = [
        "fbsource//third-party/fmt:fmt",
        "fbsource//third-party/googletest:gmock",
        "fbsource//third-party/googletest:gtest",
        "fbsource//third-party/re2:re2",
        "//fb303:service_data",
        "//fboss/agent:address_utils",
        "//fboss/agent:agent_dir_util",
        "//fboss/agent:agent_features",
        "//fboss/agent:apply_thrift_config",
        "//fboss/agent:asic_utils",
        "//fboss/agent:core",
        "//fboss/agent:ecmp_resource_manager",
        "//fboss/agent:fboss-error",
        "//fboss/agent:fboss-types",
        "//fboss/agent:fib_helpers",
        "//fboss/agent:handler",
        "//fboss/agent:hw_asic_table",
        "//fboss/agent:hwswitch_matcher",
        "//fboss/agent:load_agent_config",
        "//fboss/agent:packet",
        "//fboss/agent:route_update_wrapper",
        "//fboss/agent:sflow_shim_utils",
        "//fboss/agent:stats",
        "//fboss/agent:switch_config-cpp2-types",
        "//fboss/agent:switchid_scope_resolver",
        "//fboss/agent:utils",
        "//fboss/agent:voq_utils",
        "//fboss/agent/hw:hardware_stats-cpp2-types",
        "//fboss/agent/hw:hw_resource_stats_publisher",
        "//fboss/agent/hw:hw_switch_fb303_stats",
        "//fboss/agent/hw/switch_asics:switch_asics",
        "//fboss/agent/hw/test:config_factory",
        "//fboss/agent/hw/test:hw_copp_utils",
        "//fboss/agent/hw/test:hw_packet_utils",
        "//fboss/agent/hw/test:prod_config_factory",
        "//fboss/agent/if:common-cpp2-types",
        "//fboss/agent/if:highfreq-cpp2-types",
        "//fboss/agent/if:hw_ctrl-cpp2-clients",
        "//fboss/agent/packet:ether_type",
        "//fboss/agent/packet:packet",
        "//fboss/agent/packet:packet_factory",
        "//fboss/agent/packet:pktutil",
        "//fboss/agent/packet:sflow_structs",
        "//fboss/agent/state:label_forwarding_action",
        "//fboss/agent/state:nodebase",
        "//fboss/agent/state:state",
        "//fboss/agent/state:state_utils",
        "//fboss/agent/test:agent_ensemble",
        "//fboss/agent/test:agent_hw_test",
        "//fboss/agent/test:ecmp_helper",
        "//fboss/agent/test:production_features-cpp2-types",
        "//fboss/agent/test:resourcelibutil",
        "//fboss/agent/test:route_scale_gen",
        "//fboss/agent/test:test_ensemble_if",
        "//fboss/agent/test:trunk_utils",
        "//fboss/agent/test:utils",
        "//fboss/agent/test/utils:acl_test_utils",
        "//fboss/agent/test/utils:aqm_test_utils",
        "//fboss/agent/test/utils:asic_test_utils",
        "//fboss/agent/test/utils:config_utils",
        "//fboss/agent/test/utils:copp_test_utils",
        "//fboss/agent/test/utils:dscp_marking_utils",
        "//fboss/agent/test/utils:dsf_config_utils",
        "//fboss/agent/test/utils:ecmp_dataplane_test_util",
        "//fboss/agent/test/utils:ecmp_test_utils",
        "//fboss/agent/test/utils:fabric_test_utils",
        "//fboss/agent/test/utils:invariant_test_utils",
        "//fboss/agent/test/utils:l2learn_observer_util",
        "//fboss/agent/test/utils:load_balancer_test_runner_h",
        "//fboss/agent/test/utils:load_balancer_test_utils",
        "//fboss/agent/test/utils:mac_test_utils",
        "//fboss/agent/test/utils:mirror_test_utils",
        "//fboss/agent/test/utils:multi_port_traffic_test_utils",
        "//fboss/agent/test/utils:neighbor_test_utils",
        "//fboss/agent/test/utils:network_ai_qos_utils",
        "//fboss/agent/test/utils:olympic_qos_utils",
        "//fboss/agent/test/utils:packet_snooper",
        "//fboss/agent/test/utils:pfc_test_utils",
        "//fboss/agent/test/utils:pkt_test_utils",
        "//fboss/agent/test/utils:port_stats_test_utils",
        "//fboss/agent/test/utils:port_test_utils",
        "//fboss/agent/test/utils:qos_test_utils",
        "//fboss/agent/test/utils:queue_per_host_test_utils",
        "//fboss/agent/test/utils:queue_test_utils",
        "//fboss/agent/test/utils:route_test_utils",
        "//fboss/agent/test/utils:scale_test_utils",
        "//fboss/agent/test/utils:system_scale_test_utils",
        "//fboss/agent/test/utils:traffic_policy_utils",
        "//fboss/agent/test/utils:trap_packet_utils",
        "//fboss/agent/test/utils:trunk_test_utils",
        "//fboss/agent/test/utils:udf_test_utils",
        "//fboss/agent/test/utils:voq_test_utils",
        "//fboss/lib:common_file_utils",
        "//fboss/lib:common_utils",
        "//fboss/lib/config:fboss_config_utils",
        "//folly:file_util",
        "//folly:map_util",
        "//folly:network_address",
        "//folly:scope_guard",
        "//folly/container:f14_hash",
        "//folly/executors:function_scheduler",
        "//folly/io:iobuf",
        "//folly/json:dynamic",
        "//folly/logging:logging",
        "//folly/memory:mallctl_helper",
        "//folly/testing:test_util",
        "//thrift/lib/cpp2/test:matcher",
    ],
    exported_external_deps = [
        ("boost", None, "boost_container"),
        ("boost", None, "boost_range"),
    ],
)

cpp_library(
    name = "agent_scale_test_src",
    srcs = [
        "AgentAclScaleTests.cpp",
        "AgentEcmpScaleTests.cpp",
        "AgentRouteScaleTests.cpp",
        "AgentVoqSwitchEcmpCompressionTests.cpp",
        "AgentVoqSwitchScaleTests.cpp",
    ],
    headers = [
        "AgentVoqSwitchFullScaleDsfTests.h",
        "AgentVoqSwitchTests.h",
    ],
    link_whole = True,
    exported_deps = [
        "//fboss/agent:asic_utils",
        "//fboss/agent:core",
        "//fboss/agent:ecmp_resource_manager",
        "//fboss/agent:fboss-error",
        "//fboss/agent/hw/test:config_factory",
        "//fboss/agent/packet:packet_factory",
        "//fboss/agent/test:agent_hw_test",
        "//fboss/agent/test:ecmp_helper",
        "//fboss/agent/test:production_features-cpp2-types",
        "//fboss/agent/test:route_scale_gen",
        "//fboss/agent/test/utils:acl_test_utils",
        "//fboss/agent/test/utils:copp_test_utils",
        "//fboss/agent/test/utils:dsf_config_utils",
        "//fboss/agent/test/utils:ecmp_resource_mgr_test_utils",
        "//fboss/agent/test/utils:load_balancer_test_utils",
        "//fboss/agent/test/utils:network_ai_qos_utils",
        "//fboss/agent/test/utils:scale_test_utils",
        "//fboss/agent/test/utils:voq_test_utils",
        "//folly:network_address",
    ],
)

cpp_library(
    name = "agent_multi_node_src",
    srcs = [
        "MultiNodeAgentVoqSwitchTests.cpp",
    ],
    link_whole = True,
    exported_deps = [
        "//fboss/agent:asic_utils",
        "//fboss/agent/test:agent_hw_test",
        "//fboss/lib/multinode:multi_node_util",
    ],
)

cpp_library(
    name = "agent_hw_provision_test_src",
    srcs = [
        "AgentHwPtpTcProvisionTests.cpp",
    ],
    link_whole = True,
    exported_deps = [
        "//fboss/agent:asic_utils",
        "//fboss/agent/packet:packet",
        "//fboss/agent/packet:packet_factory",
        "//fboss/agent/test:agent_hw_test",
        "//fboss/agent/test:ecmp_helper",
        "//fboss/agent/test/utils:config_utils",
        "//fboss/agent/test/utils:packet_snooper",
        "//fboss/agent/test/utils:pkt_test_utils",
        "//fboss/agent/test/utils:trap_packet_utils",
        "//folly:network_address",
    ],
)

cpp_binary(
    name = "multi_switch_agent_hw_test",
    srcs = [
        "MultiSwitchAgentHwTest.cpp",
    ],
    deps = [
        "fbsource//third-party/googletest:gtest",
        ":agent_hw_test_src",
        "//fboss/agent:setup_thrift_prod",  # @manual
        "//fboss/agent/test:agent_hw_test",
        "//fboss/agent/test:multi_switch_agent_ensemble",
        "//folly/logging:init",
    ],
)

[
    cpp_binary(
        name = "bcm_agent_hw_test{}".format(to_impl_suffix(sdk)),
        srcs = [
            "BcmAgentHwTest.cpp",
        ],
        linker_flags = [
            "--export-dynamic",
            "--unresolved-symbols=ignore-all",
        ],
        versions = to_versions(sdk),
        deps = [
            "fbsource//third-party/googletest:gtest",
            ":agent_hw_test_src",
            "//fboss/agent:main-bcm",
            "//fboss/agent:setup_thrift_prod",
            "//fboss/agent/hw/bcm/tests:agent_hw_test_thrift_handler",
            "//fboss/agent/hw/bcm/tests:bcm_acl_utils",
            "//fboss/agent/hw/bcm/tests:bcm_aqm_utils",
            "//fboss/agent/hw/bcm/tests:bcm_copp_utils",
            "//fboss/agent/hw/bcm/tests:bcm_ecmp_utils",
            "//fboss/agent/hw/bcm/tests:bcm_linkstate_toggler",
            "//fboss/agent/platforms/wedge:platform",
            "//fboss/agent/test:agent_hw_test",
            "//fboss/agent/test:mono_agent_ensemble",
        ],
        external_deps = [
            "gflags",
            "glog",
        ],
    )
    for sdk in BCM_SDKS
]

_sai_agent_hw_test_prefix = "sai_agent_hw_test"

[
    cpp_binary(
        name = to_impl_bin_name(sai_impl, _sai_agent_hw_test_prefix),
        srcs = [
            "SaiAgentHwTest.cpp",
        ],
        link_group_map = get_link_group_map(
            to_impl_bin_name(sai_impl, _sai_agent_hw_test_prefix),
            sai_impl,
        ),
        linker_flags = [
            "--export-dynamic",
            "--unresolved-symbols=ignore-all",
            "--rpath='$ORIGIN'",
        ],
        versions = {
            sai_impl.sdk_name: sai_impl.version,
        },
        deps = [
            ":agent_hw_test_src",
            "//fboss/agent:main-sai-{}".format(to_impl_lib_name(sai_impl)),
            "//fboss/agent/hw/sai/hw_test:sai_acl_utils{}".format(to_impl_suffix(sai_impl)),
            "//fboss/agent/hw/sai/hw_test:sai_copp_utils{}".format(to_impl_suffix(sai_impl)),
            "//fboss/agent/platforms/sai:{}".format(sai_switch_dependent_name("sai_platform", sai_impl, True)),
            "//fboss/agent:setup_thrift_prod",
            "//fboss/agent/test:agent_hw_test",
            "//fboss/agent/test:linkstate_toggler",
            "//fboss/agent/test:mono_agent_ensemble",
            "//fboss/agent/hw/sai/hw_test:{}".format(sai_switch_dependent_name("agent_hw_test_thrift_handler", sai_impl, True)),
            "//fboss/agent:setup_thrift_prod",
        ] + (["//fboss/agent/platforms/sai:bcm-required-symbols"] if sai_impl.name == "fake" or sai_impl.name == "leaba" else []),
    )
    for sai_impl in SAI_BRCM_IMPLS + SAI_LEABA_IMPLS + SAI_NVIDIA_IMPLS
]

_sai_agent_scale_test_prefix = "sai_agent_scale_test"

[
    cpp_binary(
        name = to_impl_bin_name(sai_impl, _sai_agent_scale_test_prefix),
        srcs = [
            "SaiAgentHwTest.cpp",
        ],
        link_group_map = get_link_group_map(
            to_impl_bin_name(sai_impl, _sai_agent_scale_test_prefix),
            sai_impl,
        ),
        linker_flags = [
            "--export-dynamic",
            "--unresolved-symbols=ignore-all",
            "--rpath='$ORIGIN'",
        ] if sai_impl.name == "nvda" else [],
        versions = {
            sai_impl.sdk_name: sai_impl.version,
        },
        deps = [
            "fbsource//third-party/googletest:gtest",
            ":agent_scale_test_src",
            "//fboss/agent:setup_thrift_prod",
            "//fboss/agent/test:agent_hw_test",
            "//fboss/agent:main-sai-{}".format(to_impl_lib_name(sai_impl)),
            "//fboss/agent/hw/sai/hw_test:sai_acl_utils{}".format(to_impl_suffix(sai_impl)),
            "//fboss/agent/platforms/sai:{}".format(sai_switch_dependent_name("sai_platform", sai_impl, True)),
            "//fboss/agent/test:mono_agent_ensemble",
            "//fboss/agent/hw/sai/hw_test:{}".format(sai_switch_dependent_name("agent_hw_test_thrift_handler", sai_impl, True)),
            "//fboss/agent:setup_thrift_prod",
        ] + (["//fboss/agent/platforms/sai:bcm-required-symbols"] if sai_impl.name == "fake" or sai_impl.name == "leaba" else []),
    )
    for sai_impl in SAI_BRCM_IMPLS + SAI_LEABA_IMPLS + SAI_NVIDIA_IMPLS
]

_sai_agent_multi_node_test_prefix = "sai_agent_multi_node"

[
    cpp_binary(
        name = to_impl_bin_name(sai_impl, _sai_agent_multi_node_test_prefix),
        srcs = [
            "SaiAgentHwTest.cpp",
        ],
        link_group_map = get_link_group_map(
            to_impl_bin_name(sai_impl, _sai_agent_multi_node_test_prefix),
            sai_impl,
        ),
        linker_flags = [
            "--export-dynamic",
            "--unresolved-symbols=ignore-all",
            "--rpath='$ORIGIN'",
        ] if sai_impl.name == "nv_sai_sdk" else [],
        versions = {
            sai_impl.sdk_name: sai_impl.version,
        },
        deps = [
            "fbsource//third-party/googletest:gtest",
            ":agent_multi_node_src",
            "//fboss/agent:main-sai-{}".format(to_impl_lib_name(sai_impl)),
            "//fboss/agent/platforms/sai:{}".format(sai_switch_dependent_name("sai_platform", sai_impl, True)),
            "//fboss/agent/test:mono_agent_ensemble",
            "//fboss/agent/hw/sai/hw_test:{}".format(sai_switch_dependent_name("agent_hw_test_thrift_handler", sai_impl, True)),
            "//fboss/agent:setup_thrift_test",
        ] + (["//fboss/agent/platforms/sai:bcm-required-symbols"] if sai_impl.name == "fake" or sai_impl.name == "leaba" else []),
    )
    for sai_impl in SAI_BRCM_DNX_IMPLS
]

_sai_agent_provision_test_prefix = "sai_agent_provision_test"

[
    cpp_binary(
        name = to_impl_bin_name(sai_impl, _sai_agent_provision_test_prefix),
        srcs = [
            "SaiAgentHwTest.cpp",
        ],
        link_group_map = get_link_group_map(
            to_impl_bin_name(sai_impl, _sai_agent_provision_test_prefix),
            sai_impl,
        ),
        linker_flags = [
            "--export-dynamic",
            "--unresolved-symbols=ignore-all",
            "--rpath='$ORIGIN'",
        ] if sai_impl.name == "nvda" else [],
        versions = {
            sai_impl.sdk_name: sai_impl.version,
        },
        deps = [
            "fbsource//third-party/googletest:gtest",
            ":agent_hw_provision_test_src",
            "//fboss/agent:setup_thrift_prod",
            "//fboss/agent/test:agent_hw_test",
            "//fboss/agent:main-sai-{}".format(to_impl_lib_name(sai_impl)),
            "//fboss/agent/platforms/sai:{}".format(sai_switch_dependent_name("sai_platform", sai_impl, True)),
            "//fboss/agent/test:mono_agent_ensemble",
            "//fboss/agent/hw/sai/hw_test:{}".format(sai_switch_dependent_name("agent_hw_test_thrift_handler", sai_impl, True)),
            "//fboss/agent:setup_thrift_prod",
        ] + (["//fboss/agent/platforms/sai:bcm-required-symbols"] if sai_impl.name == "fake" or sai_impl.name == "leaba" else []),
    )
    for sai_impl in SAI_LEABA_IMPLS
]
